Fix ordering of time-varying statespace matices for UnivariateFilter's kalman step#653
Merged
jessegrabowski merged 2 commits intopymc-devs:mainfrom Apr 25, 2026
Merged
Conversation
Member
|
Great catch -- thanks for this! I think it makes sense to also add the missing value handling if you don't object. Also make sure you install and run pre-commit in your local dev environment. It's just |
…g of time-varying statespace matrices is correct
…es in UnivariateFilter kalman_step
a74c9a8 to
f73b15c
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #653 +/- ##
===========================================
- Coverage 80.19% 24.21% -55.98%
===========================================
Files 73 73
Lines 8088 8086 -2
===========================================
- Hits 6486 1958 -4528
- Misses 1602 6128 +4526
🚀 New features to boost your workflow:
|
jessegrabowski
approved these changes
Apr 25, 2026
Member
|
Thanks @sebcroft ! Sorry it took a minute to get in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This aims to address #612
Following the error in #612, the design matrix Z should have shape (2,3) not (3,3). Intial thought is the latter shape corresponds to the transition matrix T.
In BaseFilter, the kalman_step function calls the method unpack_args to reorganise the inputs into a standardized order. This is needed because when the user specifies time varying matrices, the kalman_step arguments (y, a, P, c, d, T, Z, R, H, Q) can get 'mixed-up' depending on which variables are assigned to the sequences and non_sequences in pytensor.scan.
UnivariateFilter redefines the kalman_step function and does not currently call unpack_args. Therefore, when the selection matrix R is the only time varying matrix (as in #612) it gets sent to the sequences/start (with y) and shifts the remaining args up by 1. As a result, T is incorrectly used for the Z input, which aligns with the shape error.
When all matrices are time-invariant unpack_args is not required because the correct ordering is preserved -- this is why UnivariateFilter works for the time-invariant matrices as mentioned in #612.
To address this I have added the method unpack_args to the kalman_step in UnivariateFilter.
Some extra stuff:
The above changes should hopefully be enough, but to keep things consistent with the BaseFilter you could possibly deal with the nan values using handle_missing_values instead i.e.:
I'm happy to add this if you think it's best!